package com.ruoyi.system.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.modbus.utils.CacheUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IotDeviceMapper;
import com.ruoyi.system.domain.IotDevice;
import com.ruoyi.system.service.IIotDeviceService;

/**
 * 设备Service业务层处理
 * 公司名称：未可自动化 <br/>
 * 作者：weikeauto <br/>
 */
@Service
public class IotDeviceServiceImpl implements IIotDeviceService 
{
    @Autowired
    private IotDeviceMapper iotDeviceMapper;

    /**
     * 查询设备
     * 
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public IotDevice selectIotDeviceById(Long id)
    {
        return iotDeviceMapper.selectIotDeviceById(id);
    }

    /**
     * 查询设备列表
     * 
     * @param iotDevice 设备
     * @return 设备
     */
    @Override
    @DataScope(deptAlias = "sd")
    public List<IotDeviceVo> selectIotDeviceList(IotDevice iotDevice)
    {
        List<IotDeviceVo> iotDeviceVoList = iotDeviceMapper.selectNewIotDeviceList(iotDevice);
        for (IotDeviceVo iotDeviceVo : iotDeviceVoList) {
            if (CacheUtils.get(iotDeviceVo.getSn()) != null) {
                iotDeviceVo.setDeviceStatus(0L);
            } else {
                iotDeviceVo.setDeviceStatus(1L);
            }
        }
        return iotDeviceVoList;
    }

    /**
     * 新增设备
     * 
     * @param iotDevice 设备
     * @return 结果
     */
    @Override
    public int insertIotDevice(IotDevice iotDevice)
    {
        // 数据重复性校验
        int deviceNameCount  = iotDeviceMapper.selectDeviceByName(iotDevice.getDeviceName());
        if(deviceNameCount>0){
            throw new ServiceException("设备名称重复");
        }
        iotDevice.setCreateTime(DateUtils.getNowDate());
        return iotDeviceMapper.insertIotDevice(iotDevice);
    }

    /**
     * 修改设备
     * 
     * @param iotDevice 设备
     * @return 结果
     */
    @Override
    public int updateIotDevice(IotDevice iotDevice)
    {
        iotDevice.setUpdateTime(DateUtils.getNowDate());
        return iotDeviceMapper.updateIotDevice(iotDevice);
    }

    /**
     * 批量删除设备
     * 
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteIotDeviceByIds(Long[] ids)
    {
        return iotDeviceMapper.deleteIotDeviceByIds(ids);
    }

    /**
     * 删除设备信息
     * 
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteIotDeviceById(Long id)
    {
        return iotDeviceMapper.deleteIotDeviceById(id);
    }

    @Override
    public List<IotDeviceVo> selectOnLineIotDeviceList() {
        return iotDeviceMapper.selectOnLineIotDeviceList();
    }

    @Override
    public List<IotVariableValueVo> variableValueList(Long deviceTemplateId,Long deviceId,String variableName,Integer variableType) {
        String userRole = "";
        for (SysRole sysRole : SecurityUtils.getLoginUser().getUser().getRoles()) {
            if ("admin".equals(sysRole.getRoleKey())) {
                userRole = "admin";
            }
        }

        return iotDeviceMapper.variableValueList(deviceTemplateId, deviceId, variableName, variableType, userRole);
    }

    @Override
    public List<IotVariableVo> variableList(Long deviceTemplateId) {
        return iotDeviceMapper.variableList(deviceTemplateId);
    }

    @Override
    public IotDeviceVo selectIotDeviceByDeviceId(Long deviceId) {
        return iotDeviceMapper.selectIotDeviceByDeviceId(deviceId);
    }

    @Override
    public IotVariableVo selectVariableById(Long id) {
        return iotDeviceMapper.selectVariableById(id);
    }

    @Override
    public List<IotVariableValueVo> variableValueHisList(String deviceName, String variableName, Integer variableType, LocalDateTime startTime, LocalDateTime endTime) {
        return iotDeviceMapper.variableValueHisList(deviceName, variableName, variableType,startTime,endTime);
    }

    @Override
    public List<IotDeviceVo> selectAllIotDeviceList( Long deptId) {
        List<IotDeviceVo> iotDeviceVoList = iotDeviceMapper.selectAllIotDeviceList(deptId);
        for (IotDeviceVo iotDeviceVo : iotDeviceVoList) {
            if (CacheUtils.get(iotDeviceVo.getSn()) != null) {
                iotDeviceVo.setDeviceStatus(0L);
            } else {
                iotDeviceVo.setDeviceStatus(1L);
            }
        }
        return iotDeviceVoList;
    }

    @Override
    public List<IotVariableValueVo> dataStatistics(Long deptId) {
        return iotDeviceMapper.dataStatistics(deptId);
    }

    @Override
    public List<OrgDevice> orgDeviceCount() {
        return iotDeviceMapper.orgDeviceCount();
    }

    @Override
    public List<OrgDevice> orgGatewayCount() {
        return iotDeviceMapper.orgGatewayCount();
    }

}
